package com.aizhixin.lab.project.course.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.project.course.entity.TeacherModuleTask;
import com.aizhixin.lab.project.course.entity.TeacherModuleTaskFile;
import com.aizhixin.lab.project.course.entity.TeacherProject;
import com.aizhixin.lab.project.course.entity.TeacherProjectModule;
import com.aizhixin.lab.project.course.repository.TeacherModuleTaskFileRepository;
import com.aizhixin.lab.project.course.repository.TeacherModuleTaskRepository;
import com.aizhixin.lab.project.course.repository.TeacherProjectModuleRepository;
import com.aizhixin.lab.project.course.repository.TeacherProjectRepository;
import com.aizhixin.lab.project.course.uilt.FileType;
import com.aizhixin.lab.project.template.core.TaskFileType;
import com.aizhixin.lab.project.template.data.ModuleData;
import com.aizhixin.lab.project.template.data.ModuleTaskData;
import com.aizhixin.lab.project.template.domain.ModuleTaskFileDomain;
import com.aizhixin.lab.project.template.domain.ProjectModuleDomain;
import com.aizhixin.lab.project.template.domain.ProjectRoleDomain;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.ProjectModule;
import com.aizhixin.lab.project.template.entity.ProjectRole;
import com.aizhixin.lab.project.template.service.KnowledgePointService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class TeacherProjectModuleService {
    @Autowired
    private TeacherProjectModuleRepository moduleRepository;
    @Autowired
    private TeacherModuleTaskRepository teacherModuleTaskRepository;
    @Autowired
    private TeacherModuleTaskFileRepository fileRepository;
    @Autowired
    private TeacherProjectRepository projectRepository;
    @Autowired
    private TeacherModuleTaskService teacherModuleTaskService;
    @Autowired
    private TeacherTaskWorkOrderService taskWorkOrderService;
    @Autowired
    private KnowledgePointService knowledgePointService;
    public void updateSort(List<String> ids){
        int i=1;
        for (String id : ids) {
            TeacherProjectModule projectModule = moduleRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
            projectModule.setModuleNo(i);
            moduleRepository.save(projectModule);
            i++;
        }
    }
    /**
     * 老师端获取项目下所有模块
     *
     * @param projectId
     * @return
     */
    public List<ProjectModuleDomain> findModuleByProject(String projectId) {
        List<TeacherProjectModule> moduleList = moduleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());
        List<ProjectModuleDomain> moduleDomainList = new ArrayList<>();
        for (TeacherProjectModule projectModule : moduleList) {
            ProjectModuleDomain moduleDomain = new ProjectModuleDomain();
            BeanUtils.copyProperties(projectModule, moduleDomain);
            List<ProjectRoleDomain> projectRoleDomainList = new ArrayList<>();
            for (ProjectRole role : projectModule.getProjectRoleList()) {
                ProjectRoleDomain roleDomain = new ProjectRoleDomain();
                BeanUtils.copyProperties(role, roleDomain);
                projectRoleDomainList.add(roleDomain);
            }
            moduleDomain.setProjectRoleDomainList(projectRoleDomainList);
            moduleDomainList.add(moduleDomain);
        }
        return moduleDomainList;
    }

    public List<ModuleData> findAllModuleData(String projectId) {
        List<TeacherProjectModule> moduleList = moduleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());
        List<ModuleData> moduleDomainList = new ArrayList<>();
        for (TeacherProjectModule projectModule : moduleList) {
            ModuleData moduleDomain = new ModuleData();
            BeanUtils.copyProperties(projectModule, moduleDomain);
            List<ProjectRoleDomain> projectRoleDomainList = new ArrayList<>();
            for (ProjectRole role : projectModule.getProjectRoleList()) {
                ProjectRoleDomain roleDomain = new ProjectRoleDomain();
                BeanUtils.copyProperties(role, roleDomain);
                projectRoleDomainList.add(roleDomain);
            }
            moduleDomain.setProjectRoleDomainList(projectRoleDomainList);
            List<ModuleTaskData> task = teacherModuleTaskService.findTask(moduleDomain.getId());
            task.forEach(item -> {
                        item.setWorkOrderDomainList(taskWorkOrderService.findWorkOrderByModuleId(item.getId()));
                        item.setKnowledgePointDomainList(knowledgePointService.findKnowledgePointDomain(item.getId()));
                        List<TeacherModuleTaskFile> fileList = fileRepository.findByTaskIdAndDeleteFlag(item.getId(), DataValidity.VALID.getState());
                        List list1 = new ArrayList();
                        List list2 = new ArrayList();
                        List list3 = new ArrayList();
                        fileList.forEach(e -> {
                            ModuleTaskFileDomain moduleTaskFileDomain = new ModuleTaskFileDomain();
                            if (e.getFileType() == TaskFileType.image.getIntValue()) {
                                BeanUtils.copyProperties(e, moduleTaskFileDomain);
                                list1.add(moduleTaskFileDomain);
                            } else if (e.getFileType() == TaskFileType.teachingMaterial.getIntValue()) {
                                BeanUtils.copyProperties(e, moduleTaskFileDomain);
                                list2.add(moduleTaskFileDomain);
                            } else if (e.getFileType() == TaskFileType.taskVideo.getIntValue()) {
                                BeanUtils.copyProperties(e, moduleTaskFileDomain);
                                list3.add(moduleTaskFileDomain);
                            }
                        });
                        item.setTaskImages(list1);
                        item.setTeachingMaterial(list2);
                        item.setVideoFileList(list3);
                    }
            );
            moduleDomain.setModuleTaskDataList(task);
            moduleDomainList.add(moduleDomain);
        }
        return moduleDomainList;
    }

    /**
     * 老师端获取模块详情
     *
     * @param id
     * @return
     */
    public ProjectModuleDomain findModuleById(String id) {
        TeacherProjectModule projectModule = moduleRepository.findOne(id);
        ProjectModuleDomain moduleDomain = new ProjectModuleDomain();
        BeanUtils.copyProperties(projectModule, moduleDomain);
        List<ProjectRoleDomain> roleDomainList = new ArrayList<>();
        for (ProjectRole projectRole : projectModule.getProjectRoleList()) {
            ProjectRoleDomain roleDomain = new ProjectRoleDomain();
            BeanUtils.copyProperties(projectRole, roleDomain);
            roleDomainList.add(roleDomain);
        }
        moduleDomain.setProjectRoleDomainList(roleDomainList);
        return moduleDomain;
    }

    /**
     * 保存模块
     *
     * @param domain
     * @return
     */
    public TeacherProjectModule save(ProjectModuleDomain domain) {
        List<ProjectRole> projectRoles = new ArrayList<>();
        if (domain.getProjectRoleDomainList() != null) {
            for (ProjectRoleDomain roleDomain : domain.getProjectRoleDomainList()) {
                ProjectRole projectRole = new ProjectRole();
                BeanUtils.copyProperties(roleDomain, projectRole);
                projectRoles.add(projectRole);
            }
        }
        if (null == domain.getId() || "".equals(domain.getId())) {
            TeacherProjectModule projectModule = new TeacherProjectModule();
            BeanUtils.copyProperties(domain, projectModule);
            projectModule.setId(UUID.randomUUID().toString());
            projectModule.setCreatedDate(new Date());
            projectModule.setDeleteFlag(DataValidity.VALID.getState());
            projectModule.setModuleNo(moduleRepository.countByProjectIdAndDeleteFlag(projectModule.getProjectId(), DataValidity.VALID.getState())+1);
            projectModule.setProjectRoleList(projectRoles);
            projectModule.setTaskNum(0);
            TeacherProject teacherProject = projectRepository.findOne(domain.getProjectId());
            teacherProject.setModuleNum(teacherProject.getModuleNum() + 1);
            projectRepository.save(teacherProject);
            return moduleRepository.save(projectModule);
        }
        TeacherProjectModule one = moduleRepository.findOne(domain.getId());
        one.setProjectRoleList(projectRoles);
        domain.setTaskNum(one.getTaskNum());
        BeanUtils.copyProperties(domain, one);
        return moduleRepository.save(one);
    }

    public Map<String, Object> deleteModule(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        Integer taskNum = teacherModuleTaskRepository.countByModuleIdAndDeleteFlag(id, DataValidity.VALID.getState());
        if (taskNum > 0) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "请先删除子任务");
        } else {
            TeacherProjectModule projectModule = moduleRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
            if (!Objects.isNull(projectModule)) {
                projectModule.setDeleteFlag(DataValidity.INVALID.getState());
                TeacherProject project = projectRepository.findOne(projectModule.getProjectId());
                moduleRepository.save(projectModule);
                if (!Objects.isNull(project)) {
                    project.setModuleNum(moduleRepository.countByProjectIdAndDeleteFlag(project.getId(), DataValidity.VALID.getState()));
                    projectRepository.save(project);
                    updateSort(project.getId());
                }
                result.put(ApiReturnConstants.SUCCESS, true);
                result.put(ApiReturnConstants.DATA, "删除成功");
            } else {
                result.put(ApiReturnConstants.SUCCESS, false);
                result.put(ApiReturnConstants.DATA, "模块不存在");
            }
        }
        return result;
    }

    public void updateSort(String projectId) {
        List<TeacherProjectModule> projectModuleList = moduleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());
        for (int i = 0; i < projectModuleList.size(); i++) {
            projectModuleList.get(i).setModuleNo(i + 1);
        }
        moduleRepository.save(projectModuleList);
    }


}
